package com.newsystem.controller;


import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 文件：那个新闻的图片和视频链接 前端控制器
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/tfile")
public class TfileController {

    @Resource
    private MinioClient minioClient;
    @Value("${minio.bucket}")
    private String bucketName;

    @PostMapping("/update")
    public CommonResult<Object> upload(MultipartFile file){
        if(file==null){
            return CommonResult.failed("上传的文件为空");
        }
        String filename = MyUUIDUtils.getUUIDFileName(file.getOriginalFilename());
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(filename)
                    .stream(file.getInputStream(),file.getSize(),-1)
                    .contentType(file.getContentType()).build();
            minioClient.putObject(objectArgs);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("文件上传失败");
        }
        return CommonResult.success("http://162.14.78.140:9000"+"/"+bucketName+"/"+filename);
    }

}

